package com.eddie.libray.service;

import com.eddie.libray.LibraryDataSource;
import com.eddie.libray.entity.BookEntity;
import com.eddie.libray.utils.DataSourceUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * 管理员业务处理
 *
 * @author Eddie
 * @since 2022/09/02 09:56
 **/
public class AdminService {

    private final Scanner scanner = new Scanner(System.in);

    private final List<BookEntity> books = LibraryDataSource.books;

    /**
     * 查询所有书籍
     */
    public void list() {
        DataSourceUtils.list();
    }

    public void add() {
        BookEntity bookEntity = new BookEntity();
        System.out.println("请输入书名：");
        bookEntity.setName(scanner.next());
        System.out.println("请输入价格：");
        bookEntity.setPrice(scanner.nextDouble());
        System.out.println("请输入作者名称：");
        bookEntity.setAuthor(scanner.next());
        //设置出版时间为当前
        bookEntity.setCreateTime(new Date());
        //设置id为维护的书本数据源最新一本的id+1
        bookEntity.setId(DataSourceUtils.peek().getId() + 1);
        books.add(bookEntity);
    }

    public void update() {
        System.out.println("请输入要修改的书本编号：");
        int id = scanner.nextInt();
        //根据书籍编号查询书籍
        int index = DataSourceUtils.getBookIndexById(id);
        if (index == -1) {
            System.out.println("没有找到编号为：" + id + "的书籍");
            return;
        }

        //获取要修改的书籍信息
        BookEntity bookEntity = books.get(index);
        System.out.println("请输入要修改的选项：");
        String answer;
        do {
            updateMenu();
            answer = scanner.next();
            switch (answer) {
                case "1": {
                    System.out.println("请输入书名：");
                    //设置书名
                    bookEntity.setName(scanner.next());
                    break;
                }
                case "2": {
                    System.out.println("请输入价格：");
                    //设置价格
                    bookEntity.setPrice(scanner.nextDouble());
                    break;
                }
                case "3": {
                    System.out.println("请输入作者名称：");
                    //设置名称
                    bookEntity.setAuthor(scanner.next());
                    break;
                }
                default: {
                    System.out.println("请正确选择！");
                    break;
                }
            }
        } while (!"4".equals(answer));
        System.out.println("编号为：" + id + "的书籍修改成功");
    }

    /**
     * 修改书籍的菜单
     */
    private void updateMenu() {
        System.out.println("1、书名");
        System.out.println("2、价格");
        System.out.println("3、作者");
        System.out.println("4、保存");
    }

    public void delete() {
        System.out.println("请输入要删除的书籍编号：");
        int id = scanner.nextInt();
        //根据书籍编号查询书籍
        int index = DataSourceUtils.getBookIndexById(id);
        if (index == -1) {
            System.out.println("没有找到编号为：" + id + "的书籍");
            return;
        }
        //删除获取的下标的书籍并返回删除后的书籍
        BookEntity remove = books.remove(index);
        if (remove != null) {
            System.out.println("书名为：" + remove.getName() + "的书籍删除成功");
        } else {
            System.out.println("删除失败，未知错误");
        }
    }
}
